using System.Collections.Generic;
using System.Web.Mvc;
using Headspring.Training.Core.Model;

namespace Headspring.Training.UI.Models
{
	public class WorkOrderStatusOptionalSelectList : SelectList
	{
		private readonly string _selectedValue;

		public WorkOrderStatusOptionalSelectList(IEnumerable<WorkOrderStatus> statuses)
			: base(statuses)
		{
		}

		public WorkOrderStatusOptionalSelectList(WorkOrderStatus[] statuses,
		                                         WorkOrderStatus status)
			: this(statuses)
		{
			if (status != null)
			{
				_selectedValue = status.Key;
			}
		}

		public override IList<ListItem> GetListItems()
		{
			var listItems = new List<ListItem>();
			var anyItem = new ListItem();
			anyItem.Text = "<Any>";
			anyItem.Value = "";
			listItems.Add(anyItem);
			foreach (WorkOrderStatus status in Items)
			{
				var item = new ListItem();
				item.Text = status.ToString();
				item.Value = status.Key;

				if (status.Key == _selectedValue)
				{
					item.Selected = true;
				}

				listItems.Add(item);
			}
			return listItems;
		}
	}
}